<?php
namespace app\admin\controller;

use app\admin\controller\Base;
use think\Db;

class Goods extends Base
{
    /**
     * 分类列表
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function ClassList(){
        //记录数量
        $Count = Db::name('goods_class')->count();
        $this->assign('Count',$Count);

        //分类信息
        $ClassList = Db::name('goods_class')->where(['son'=>0])->select();
        foreach ($ClassList as $k=>$v){
            $ClassList[$k]['son'] = Db::name('goods_class')->where(['parent_id'=>$ClassList[$k]['id']])->select();
        }
        $this->assign('ClassList',$ClassList);

        return $this->fetch('ClassList');
    }

    /**
     * 分类添加
     * @return array|mixed
     */
    public function ClassAdd(){
        //Post请求操作
        if(IsPost()){
            //请求数据
            $Data = Param();

            //查看分类是否存在
            $ClassInfo = Db::name('goods_class')->where(['name'=>$Data['name']])->find();
            if(!empty($ClassInfo)){
                return ['status'=>0,'message'=>'该分类已存在，请重新输入'];
            }

            if($Data['parent_id'] != 0){
                $Data['son'] = 1;
            }

            //向表中添加数据
            if(Db::name('goods_class')->insert($Data)){
                return ['status'=>1,'message'=>'添加成功'];
            }else{
                return ['status'=>0,'message'=>'系统错误，请重试'];
            }
        }

        //父级分类信息
        $ParentClass = Db::name('goods_class')->where(['son'=>0])->select();
        $this->assign('ParentClass',$ParentClass);

        return $this->fetch('ClassAdd');
    }

    /**
     * 分类编辑
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function ClassEdit(){
        //分类信息
        $ClassInfo = Db::name('goods_class')->where(['id'=>Param('id')])->find();
        $this->assign('ClassInfo',$ClassInfo);

        //Post请求操作
        if(IsPost()){
            //请求数据
            $Data = Param();

            //查看账号是否存在
            $Where['name'] = $Data['name'];
            $Where['id'] = array('neq',$Data['id']);
            $ClassInfo = Db::name('goods_class')->where($Where)->find();
            if(!empty($ClassInfo)){
                return ['status'=>0,'message'=>'该分类已存在，请重新输入'];
            }

            if($Data['parent_id'] != 0){
                $Data['son'] = 1;
            }

            //向表中添加数据
            if(Db::name('goods_class')->update($Data)){
                return ['status'=>1,'message'=>'修改成功'];
            }else{
                return ['status'=>0,'message'=>'无修改'];
            }
        }

        //父级分类信息
        $ParentClass = Db::name('goods_class')->where(['son'=>0])->select();
        $this->assign('ParentClass',$ParentClass);

        return $this->fetch('ClassEdit');
    }

    /**
     * 分类删除
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function ClassDelete(){
        //Post请求操作
        if(IsPost()){
            //分类信息
            $ClassInfo = Db::name('goods_class')->where(['id'=>Param('id')])->find();

            //如果是顶级分类 判断下边是否拥有子级分类
            if($ClassInfo['son'] == 0){
                //下级子分类数量
                $SonCount = Db::name('goods_class')->where(['parent_id'=>$ClassInfo['id']])->count();

                if($SonCount > 0){
                    return ['status'=>0,'message'=>'该分类下有下级分类，不能删除'];
                }
            }

            //如果是子级分类 判断下边是否拥有商品
            if($ClassInfo['son'] == 1){
                //商品数量
                $GoodsCount = Db::name('goods')->where(['son_id'=>$ClassInfo['id']])->count();

                if($GoodsCount > 0){
                    return ['status'=>0,'message'=>'该分类下有商品，不能删除'];
                }
            }

            if(Db::name('goods_class')->where(['id'=>$ClassInfo['id']])->delete()){
                return ['status'=>1,'message'=>'删除成功'];
            }else{
                return ['status'=>0,'message'=>'系统错误，请重试'];
            }
        }
    }

    /**
     * 商品列表
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function GoodsList(){
        //条件查询
        $Data = Param();
        if (empty($Data)){
            $Data = [
                'son_id' => 0,
                'name' => '',
                'is_shelf' => 0,
                'goods_type' =>0,
            ];
        }
// var_dump($Data);die;
        $this->assign('Data',$Data);
        $Where = [];
        //分类查询
        if($Data['son_id'] > 0){
            $Where['son_id'] = $Data['son_id'];
        }

        //产品名称查询
        if($Data['name'] != ''){
            $Where['name'] = array('like','%'.filterEmoji($Data['name']).'%');

        }

        //上架查询
        if($Data['is_shelf'] > 0){
            $Where['is_shelf'] = $Data['is_shelf'];
        }
        
        //类别
        if($Data['goods_type'] > 0){
            $Where['goods_type'] = $Data['goods_type'];
        }

        //总记录条数
        $Count = GoodsCount($Where);
        $this->assign('Count', $Count);

        //商品信息列表
        $GoodsList = GoodsList($Where);
        $this->assign('GoodsList', $GoodsList['GoodsList']);

        //分页
        $this->assign('Page', $GoodsList['Page']);
        //级别信息
        $ClassList = Db::name('goods_class')->where(['son'=>1])->select();
        $this->assign('ClassList', $ClassList);

        return $this->fetch('GoodsList');
    }

    /**
     * 商品添加
     * @return array|mixed
     */
    public function GoodsAdd(){
        //Post请求操作
        if(IsPost()){
            //请求数据
            $Data = Param();
            if($Data['album_1'] != '' && $Data['album_2'] == '' && $Data['album_3'] == ''){
                $Data['album'] = $Data['album_1'];
            }elseif ($Data['album_1'] == '' && $Data['album_2'] != '' && $Data['album_3'] == ''){
                $Data['album'] = $Data['album_2'];
            }elseif ($Data['album_1'] == '' && $Data['album_2'] == '' && $Data['album_3'] != ''){
                $Data['album'] = $Data['album_3'];
            }elseif ($Data['album_1'] != '' && $Data['album_2'] != '' && $Data['album_3'] == '') {
                $Data['album'] = $Data['album_1'].','.$Data['album_2'];
            }elseif ($Data['album_1'] != '' && $Data['album_2'] == '' && $Data['album_3'] != '') {
                $Data['album'] = $Data['album_1'].','.$Data['album_3'];
            }elseif ($Data['album_1'] == '' && $Data['album_2'] != '' && $Data['album_3'] != '') {
                $Data['album'] = $Data['album_2'].','.$Data['album_3'];
            }elseif ($Data['album_1'] != '' && $Data['album_2'] != '' && $Data['album_3'] != '') {
                $Data['album'] = $Data['album_1'].','.$Data['album_2'].','.$Data['album_3'];
            }

            $Data['add_time'] = time();
            $Data['btime'] = strtotime($Data['btime']);
            $Data['etime'] = strtotime($Data['etime']);
            //向表中添加数据
            if(Db::name('goods')->insert($Data)){
                return ['status'=>1,'message'=>'添加成功'];
            }else{
                return ['status'=>0,'message'=>'系统错误，请重试'];
            }
        }

        //分类信息
        $ClassList = Db::name('goods_class')->where(['son'=>0])->select();
        foreach ($ClassList as $k=>$v){
            $ClassList[$k]['son'] = Db::name('goods_class')->where(['parent_id'=>$ClassList[$k]['id']])->select();
        }
        $this->assign('ClassList',$ClassList);

        return $this->fetch('GoodsAdd');
    }

    /**
     * 商品编辑
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function GoodsEdit(){
        //商品信息
        $GoodsInfo = GoodsInfo(['id'=>Param('id')]);
        $GoodsInfo['album'] = explode(",",$GoodsInfo['album']);
        if(!empty($GoodsInfo['album'][0])){
            $GoodsInfo['album_1'] = $GoodsInfo['album'][0];
        }else{
            $GoodsInfo['album_1'] = '';
        }

        if(!empty($GoodsInfo['album'][1])){
            $GoodsInfo['album_2'] = $GoodsInfo['album'][1];
        }else{
            $GoodsInfo['album_2'] = '';
        }

        if(!empty($GoodsInfo['album'][2])){
            $GoodsInfo['album_3'] = $GoodsInfo['album'][2];
        }else{
            $GoodsInfo['album_3'] = '';
        }

        $this->assign('GoodsInfo',$GoodsInfo);

        //父级分类信息
        $ClassList = Db::name('goods_class')->where(['son'=>0])->select();
        $this->assign('ClassList',$ClassList);

        //子级分类
        $ClassSon = Db::name('goods_class')->where(['son'=>1])->select();
        $this->assign('ClassSon',$ClassSon);
        
        //Post请求操作
        if(IsPost()){
            //请求数据
            $Data = Param();
            if($Data['album_1'] != '' && $Data['album_2'] == '' && $Data['album_3'] == ''){
                $Data['album'] = $Data['album_1'];
            }elseif ($Data['album_1'] == '' && $Data['album_2'] != '' && $Data['album_3'] == ''){
                $Data['album'] = $Data['album_2'];
            }elseif ($Data['album_1'] == '' && $Data['album_2'] == '' && $Data['album_3'] != ''){
                $Data['album'] = $Data['album_3'];
            }elseif ($Data['album_1'] != '' && $Data['album_2'] != '' && $Data['album_3'] == '') {
                $Data['album'] = $Data['album_1'].','.$Data['album_2'];
            }elseif ($Data['album_1'] != '' && $Data['album_2'] == '' && $Data['album_3'] != '') {
                $Data['album'] = $Data['album_1'].','.$Data['album_3'];
            }elseif ($Data['album_1'] == '' && $Data['album_2'] != '' && $Data['album_3'] != '') {
                $Data['album'] = $Data['album_2'].','.$Data['album_3'];
            }elseif ($Data['album_1'] != '' && $Data['album_2'] != '' && $Data['album_3'] != '') {
                $Data['album'] = $Data['album_1'].','.$Data['album_2'].','.$Data['album_3'];
            }

            $Data['add_time'] = time();
            if($Data['btime']){
                $Data['btime'] = strtotime($Data['btime']);
            }else{
                $Data['btime'] = $GoodsInfo['btime'];
            }
            if($Data['etime']){
                $Data['etime'] = strtotime($Data['etime']);
            }else{
                $Data['etime'] = $GoodsInfo['etime'];
            }
            // var_dump($Data);die;
            //向表中添加数据
            if(Db::name('goods')->update($Data)){
                return ['status'=>1,'message'=>'修改成功'];
            }else{
                return ['status'=>0,'message'=>'系统错误，请重试'];
            }
        }

        return $this->fetch('GoodsEdit');
    }

    /**
     * 商品上下架
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function GoodsShelf(){
        //Post请求操作
        if(IsPost()){
            //商品信息
            $GoodsInfo = GoodsInfo(['id'=>Param('id')]);

            if($GoodsInfo['is_shelf'] == 1){
                $GoodsSave['is_shelf'] = 2 ;
            }elseif($GoodsInfo['is_shelf'] == 2){
                $GoodsSave['is_shelf'] = 1 ;
            }

            //删除表中状态
            if(Db::name('goods')->where(['id'=>$GoodsInfo['id']])->update($GoodsSave)){
                return ['status'=>1,'message'=>'修改成功'];
            }else{
                return ['status'=>0,'message'=>'系统错误，请重试'];
            }
        }
    }

    /**
     * 父级分类查找对应子级分类
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function SonChoose(){
        if(IsPost()){
            $ParentID = Param('parent_id');
            $SonClass = Db::name('goods_class')->where(['parent_id'=>$ParentID])->select();

            $SonClassStr = "";
            foreach($SonClass as $k=>$v){
                $SonClassStr .= "<option value='{$v['id']}'>{$v['name']}</option>";
            }
            echo json_encode($SonClassStr);
        }
    }

    /**
     * 多图上传
     */
    public function forUpload()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('image');

        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $file->validate(['size'=>1567800000,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/goods');

        if($info){
            //返回上传图片路径
            $path = "/uploads/goods/".$info->getSaveName();
            return ['status'=>1,'message'=>$path];
        }else{
            // 上传失败获取错误信息
            return ['status'=>0,'message'=>$file->getError()];
        }
    }


    /**
     * 管理员编辑
     * @return array|mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function AdminEdit(){
        $AdminInfo = Db::name('admin')->where(['id'=>Param('id')])->find();
        //Post请求操作
        if(IsPost()){
            //请求数据
            $Data = Param();
            //向表中添加数据
            if(Db::name('admin')->update($Data)){
                return ['status'=>1,'message'=>'修改成功'];
            }else{
                return ['status'=>0,'message'=>'系统错误，请重试'];
            }
        }
        $this->assign('AdminInfo',$AdminInfo);

        //角色信息
        $Role = Db::name('admin_role')->select();
        $this->assign('Role',$Role);

        return $this->fetch('AdminEdit');
    }

    /**
     * 管理员重置密码
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function AdminPassword(){
        //Post请求操作
        if(IsPost()){
            //删除表中数据
            $Save['password'] = md5(123456);
            if(Db::name('admin')->where(['id'=>Param('id')])->update($Save)){
                return ['status'=>1,'message'=>'重置成功'];
            }else{
                return ['status'=>0,'message'=>'系统错误，请重试'];
            }
        }
    }


    /**
     * 管理员删除
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function AdminDelete(){
        //Post请求操作
        if(IsPost()){
            //删除表中数据
            if(Db::name('admin')->where(['id'=>Param('id')])->delete()){
                return ['status'=>1,'message'=>'删除成功'];
            }else{
                return ['status'=>0,'message'=>'系统错误，请重试'];
            }
        }
    }

    /**
     * 个人资料修改
     * @return array|mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function InfoEdit(){
        $AdminInfo = Db::name('admin')->where(['id'=>Param('id')])->find();
        //Post请求操作
        if(IsPost()){
            //请求数据
            $Data = Param();
            //向表中添加数据
            if(Db::name('admin')->update($Data)){
                return ['status'=>1,'message'=>'修改成功'];
            }else{
                return ['status'=>0,'message'=>'系统错误，请重试'];
            }
        }
        $this->assign('AdminInfo',$AdminInfo);
        return $this->fetch('InfoEdit');
    }

    /**
     * 修改密码
     * @return mixed
     */
    public function SavePass(){
        $AdminInfo = Db::name('admin')->where(['id'=>Param('id')])->find();
        if(IsPost()){
            //请求数据
            $Data = Param();
            if(md5($Data['old_password']) != $AdminInfo['password']){
                return ['status'=>0,'message'=>'原密码错误'];
            }
            $SaveData['password'] = md5($Data['new_password']);
            //向表中添加数据
            if(Db::name('admin')->where(['id'=>$AdminInfo['id']])->update($SaveData)){
                return ['status'=>1,'message'=>'修改成功'];
            }else{
                return ['status'=>0,'message'=>'系统错误，请重试'];
            }
        }
        return $this->fetch('SavePass');
    }
    
    //邮费设置
    public function youfei(){
        //分类信息
        $ClassList = Db::name('goods_class')->where(['son'=>0])->select();
        foreach ($ClassList as $k=>$v){
            $ClassList[$k]['son'] = Db::name('goods_class')->where(['parent_id'=>$ClassList[$k]['id']])->select();
        }
        $this->assign('ClassList',$ClassList);
        return $this->fetch('youfei');
    }
}